<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Admin_EventController extends Zend_Controller_Action
{

    private $_eventService;
    private $_shiftService;

    public function init()
    {
        $this->_eventService = $this->_helper->getService('Core_Service_Event');
        $this->_shiftService = $this->_helper->getService('Core_Service_Shift');
    }

    public function indexAction()
    {
        $this->_helper->acl()->isAllowed('service:event', 'fetch');

        $filterContent = array(
            'future'    =>  'Fremtidige',
            'past'      =>  'Tidligere',
            'all'       =>  'Alle',
         );
         $filterForm = $this->view->filterForm = $this->view->filterForm($filterContent);
         $filterForm->setAction($this->view->url(array('module' => 'admin', 'controller' => 'event'), null, true));

         $request = $this->getRequest();
         if ($request->isGet() && $this->_getParam('filter', false) && $filterForm->isValid($request->getQuery())) {
             $value = $filterForm->getValue('filter');
             switch ($value) {
                 case 'future':
                     $this->view->events = $this->_eventService->fetchAllFuture(false);
                     break;
                 case 'past':
                     $this->view->events = $this->_eventService->fetchAllPast(false);
                     break;
                 case 'all':
                     $this->view->events = $this->_eventService->fetchAll(false);
                     break;
             }
         }

         if (!isset($this->view->events)) {
             $this->view->events = $this->_eventService->fetchAllFuture(false);
         }
    }

    public function createAction()
    {
        $this->_helper->acl()->isAllowed('service:event', 'create');

        $form = new Admin_Form_Event_Add();
        $this->view->form = $form;

        $request = $this->getRequest();
        if($request->isPost() && $form->isValid($request->getPost())) {
            $data = $form->getValues();

            $event = new Core_Model_Event();

            $fromDateTime = $data['from_date'] . ' ' . $data['from_time'];
            $from_date = new RedCross_Date($fromDateTime, RedCross_Date::NO_DATE_FMT . ' ' . RedCross_Date::NO_TIME_FMT);
            $data['from_date'] = $from_date->export();

            $toDateTime = $data['to_date'] . ' ' . $data['to_time'];
            $to_date = new RedCross_Date($toDateTime, RedCross_Date::NO_DATE_FMT . ' ' . RedCross_Date::NO_TIME_FMT);
            $data['to_date'] = $to_date->export();

            $event->fromArray($data);
            // Set the user currently logged in as the creator of the event
            $authService = RedCross_Factory::getService('Core_Service_Auth');
            $identity = $authService->getIdentity();
            $event->member_id = $identity->member_id;

            // Set the timestap for when the event was created
            $date = new RedCross_Date();
            $event->created_at = $date->export();

            $event->type = $data['type'];

            $this->_eventService->create($event);

            return $this->_helper->redirector('view', 'event', 'core', array('id' => $event->id));
        }
    }

    public function editAction()
    {
        $this->_helper->acl()->isAllowed('service:event', 'edit');

        $eventId = $this->_getParam('id');

        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($eventId)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $eventId = (int) $eventId;
        $this->view->eventId = $eventId;

        $event = $this->_eventService->fetch($eventId);
        if (!$event) {
            throw new RedCross_Exception_NotFound();
        }

        $fillFormValues = array();
        $fillFormValues['type'] = $event->type;
        $fillFormValues['name'] = $event->name;
        $fillFormValues['description'] = $event->description;
        $fillFormValues['location'] = $event->location;
        $fillFormValues['contact_name'] = $event->contact_name;
        $fillFormValues['contact_phone'] = $event->contact_phone;

        $fromDate = new RedCross_Date();
        $fromDate->import($event->from_date);
        $fillFormValues['from_date'] = $fromDate->getDateStr();
        $fillFormValues['from_time'] = $fromDate->getTimeStr();

        $toDate = new RedCross_Date();
        $toDate->import($event->to_date);
        $fillFormValues['to_date'] = $toDate->getDateStr();
        $fillFormValues['to_time'] = $toDate->getTimeStr();

        $fillFormValues['requested_resource'] = $event->requested_resource;

        $form = new Admin_Form_Event_Edit();
        $form->populate($fillFormValues);

        $request = $this->getRequest();

        if($request->isPost() && $form->isValid($request->getPost())) {
            $data = $form->getValues();

            $fromDateTime = $data['from_date'] . ' ' . $data['from_time'];
            $from_date = new RedCross_Date($fromDateTime, RedCross_Date::NO_DATE_FMT . ' ' . RedCross_Date::NO_TIME_FMT);
            $data['from_date'] = $from_date->export();

            $toDateTime = $data['to_date'] . ' ' . $data['to_time'];
            $to_date = new RedCross_Date($toDateTime, RedCross_Date::NO_DATE_FMT . ' ' . RedCross_Date::NO_TIME_FMT);
            $data['to_date'] = $to_date->export();

            $event->fromArray($data);

            $this->_eventService->save($event);

            $this->view->flashMessage()->addMessage('Oppdatering utført', RedCross_View_Helper_FlashMessage::SUCCESS);
            $this->_helper->redirector('view', 'event', 'core', array('id' => $event->id));
        }
        $this->view->form = $form;
    }

    public function deleteAction()
    {
        $this->_helper->acl()->isAllowed('service:event', 'delete');

        $eventId = $this->_getParam('id');

        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($eventId)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $eventId = (int) $eventId;

        $event = $this->_eventService->fetch($eventId);
        if (is_null($event))
        {
            throw new RedCross_Exception_NotFound();
        }
        $this->view->event = $event;

        $deleteForm = new Admin_Form_Event_Delete();
        $request = $this->getRequest();
        if($request->isPost() && $deleteForm->isValid($request->getPost()))
        {
            $this->_eventService->delete($eventId);

            $this->view->flashMessage()->addMessage('Slettet et arrangement', RedCross_View_Helper_FlashMessage::SUCCESS);
            $this->_helper->redirector('index');
            return;
        }
        $this->view->deleteForm = $deleteForm;
    }
}
